﻿using UnityEngine;
using System.Collections;

public class CubeInteract : MonoBehaviour 
{
	public float life = 3.0f;
	
	void OnCollisionEnter(Collision other) 
	{
		switch(other.collider.tag)
		{
		case "Bullet":	
			life -= 2;
			
			break;
		case "BulletMachine":	
			life -= 0.5f;
			
			break;
		case "BulletSniper":	
			life -= 3;
			
			break;
		case "BulletLauncher":	
			life -= 3;
			
			break;
		case "BulletHoming":	
			life -= 3;
			
			break;
		}
		
		if (life > 1 && life < 2)
		{
			renderer.material.color = new Color (130.0f, 130.0f, 130.0f);
		}
		else if (life > 0 && life < 1)
		{
			renderer.material.color = new Color (43.0f, 43.0f, 43.0f);
		}
		else if(life <= 0)
		{
			StartCoroutine("CubeFall");
		}
	}
	
	IEnumerator CubeFall()
	{
		renderer.material.color = Color.red;
		
		float time = 0f;
		while ( time < 6f)
		{
			time += Timing.CustomDeltaTime();
			transform.position = Vector3.Lerp( transform.position, Vector3.zero, time / 6f);
			transform.localScale = Vector3.Lerp( transform.localScale, Vector3.zero, time / 6f);
			
			yield return null;
		}
	}
}
